<?php
/**
 * @email wei@alipay168.cn
 * @author 小韦
 * @link http://blog.alipay168.cn
 * @Date: 2020/10/2 9:57
 */
namespace app\server;

use app\common\library\wechat\WxBase;
use app\common\model\App as AppModel;



class LiveServer
{
    public static function get_room_list($param = [])
    {
        return ['code' => -1, 'msg' => '已关闭'];
        $wxConfig = AppModel::getAppCache($param['app_id']);
        $wxBase = new WxBase($wxConfig['appkey'], $wxConfig['app_secret']);
        $url = 'https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=' . $wxBase->getAccessToken();
        $data = [
            'start' => 0,
            'limit' => 10
        ];
        $res = json_decode($wxBase->post($url, json_encode($data)), true);
        if (!empty($res['errcode'])) {
            EmailServer::send_mail(['xcx@alipay168.cn'], '微信小程序获取直播信息错误', $res['errmsg']);
            return ['code' => -1, 'msg' => $res['errmsg']];
        }
        if (empty($res['room_info'])) {
            return ['code' => 1, 'msg' => '暂无直播'];
        }
        $status = [101 => '直播中', 102 => '未开始', 103 => '已结束', 104 => '禁播', 105 => '暂停', 106 => '异常', 107 => '已过期'];
        foreach ($res['room_info'] as &$v) {
            $v['status'] = $status[$v['live_status']];
            if ($v['live_status'] == 102) {
                $time = $v['start_time'] - time();
                if ($time > 3600) {
                    $v['timeInfo'] = '开始直播:' . intval($time / 3600) . '小时' . intval($time % 3600 / 60) . '分';
                } else {
                    $v['timeInfo'] = '开始直播:' . intval(($time / 60)) . '分';
                }
                if ($time<0){
                    $v['timeInfo'] = '主播准备中';
                }
            }
            if (date('d',$v['start_time'])==date('d',$v['end_time'])){
                $v['timeSet'] = date('d日H:i',$v['start_time']).'~'.date('H:i',$v['end_time']);
            }else{
                $v['timeSet'] = date('d日H:i',$v['start_time']).'~'.date('d日H:i',$v['end_time']);
            }
            $v['start_time'] = date('m月d H:i', $v['start_time']);
            $v['end_time'] = date('m月d H:i', $v['end_time']);
        }
        unset($v);
        $ret = $res['room_info'];
        return ['code' => 0, 'msg' => 'ok', 'data' => $ret];
    }
}